comparing Cookies and LocalStorage

Cookies do store data locally in the users browser but they operate differently from localStorage . Cookies are data stored by...

comparing Cookies and LocalStorage

Sagar
February 03, 2024

Comparing Cookies and LocalStorage🔗

Comparing Cookies and LocalStorage

Cookies do store data locally in the user's browser, but they operate differently from localStorage. Let's clarify the differences and explore how each can be used, including whether cookies use localStorage or not.

Cookies🔗

  • Storage Mechanism: Cookies are data stored by the web browser and associated with a specific domain. They are sent to the server with every HTTP request made to that domain, allowing the server to maintain state or sessions.
  • Use Cases: Commonly used for session management, user tracking, and personalization.
  • Access: Both server-side and client-side scripts can read and write cookies.
  • Size Limit: Cookies are limited in size (around 4KB per cookie) and number (varies by browser, but around 20+ per domain).

Example: Storing Cart Data in Cookies🔗

// Setting a cookie with JavaScript on the client-side
document.cookie = "cart=productId1,productId2,productId3; path=/; expires=Fri, 31 Dec 9999 23:59:59 GMT";

// Reading the cookie
const cartCookie = document.cookie.split('; ').find(row => row.startsWith('cart='));
const cart = cartCookie ? cartCookie.split('=')[1].split(',') : [];
console.log(cart); // ["productId1", "productId2", "productId3"]

In this example, product IDs are stored in a cookie as a comma-separated list. This method has limitations due to the size constraint of cookies and because cookies are sent with every HTTP request, potentially increasing network traffic unnecessarily.

LocalStorage🔗

  • Storage Mechanism: Part of the Web Storage API, providing a way to store data locally within the user's browser. Unlike cookies, data stored in localStorage is not sent with every HTTP request.
  • Use Cases: Ideal for storing larger amounts of data that do not need to be transmitted with every server request, such as preferences, settings, or even shopping cart contents.
  • Access: Only accessible by client-side scripts.
  • Size Limit: The storage limit is much larger than cookies (at least 5MB).

Example: Storing Cart Data in LocalStorage🔗

// Adding items to localStorage
localStorage.setItem('cart', JSON.stringify(['productId1', 'productId2', 'productId3']));

// Retrieving items from localStorage
const cart = JSON.parse(localStorage.getItem('cart'));
console.log(cart); // ["productId1", "productId2", "productId3"]

In this example, an array of product IDs is stored as a JSON string in localStorage. This method allows for storing more data than cookies and is more suitable for large or complex data that doesn't need to be sent to the server with every request.

Cookies vs. LocalStorage🔗

  • Transmission: Cookies are automatically sent to the server with every request, which can be useful for server-side reading but increases the amount of data transmitted. localStorage is purely client-side storage and not transmitted with HTTP requests.
  • Accessibility: Cookies can be accessed by both the server and client, whereas localStorage is accessible only by client-side scripts.
  • Persistence: Both cookies and localStorage persist across browser sessions, but cookies can be set to expire on a specific date.
  • Security: Data in cookies can be secured by setting the HttpOnly flag, making them inaccessible via JavaScript and protecting against XSS attacks. localStorage is accessible through JavaScript, which can be a vector for XSS attacks if the data is not properly handled.

COMING SOON ! ! !

Till Then, you can Subscribe to Us.

Get the latest updates, exclusive content and special offers delivered directly to your mailbox. Subscribe now!

ClassFlame – Where Learning Meets Conversation! offers conversational-style books in Computer Science, Mathematics, AI, and ML, making complex subjects accessible and engaging through interactive learning and expertly curated content.


© 2024 ClassFlame. All rights reserved.